<?php

namespace app\api\service;

use think\facade\Db;

class GoldService
{

    /**
     * 获取用户金豆
     * */
    public static function getUserGold($user_id)
    {
        $gold = Db::name('user')->where('user_id',$user_id)->value('gold');

        return $gold;
    }

    /**
     * 积分兑换金豆
     * */
    public static function reedGold($data)
    {

        Db::startTrans();
        try {
            Db::name('user')->where('user_id',$data['user_id'])->dec('integral',$data['change_integral'])->update();
            Db::name('user')->where('user_id',$data['user_id'])->inc('gold',$data['gold_num'])->update();
            Db::name('user_integral_detail')->insert([
                'user_id' => $data['user_id'],
                'change_desc' => '积分兑换金豆',
                'change_type' => 2,
                'change_integral' => $data['change_integral'],
                'change_time' => time()
            ]);
            Db::name('user_gold_detail')->insert([
                'user_id' => $data['user_id'],
                'change_desc' =>  '积分兑换金豆',
                'change_type' => 1,
                'change_gold' => $data['gold_num'],
                'change_time' => time()
            ]);
            Db::commit();
            return  true;
        }catch (\Exception $e){
            Db::rollback();
            return  false;
        }
    }


}